package com.jinyang.baserouter.pretreatment

import android.content.Context
import android.widget.Toast
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.service.PretreatmentService
import com.jinyang.baserouter.PATH_ACTIVITY_LOGIN
import com.jinyang.baserouter.PATH_ACTIVITY_WEB
import com.jinyang.baserouter.log
import com.jinyang.baserouter.routerNavigation
import com.jinyang.baserouter.utils.ApplicationUtil

/**
 * @Author:         LiuJinYang
 * @CreateDate:     2021/12/9
 * @Description:预处理服务
 */
@Route(path = "/pretreatment/test")
class TestPretreatmentServiceImpl : PretreatmentService {
    override fun init(context: Context?) {
        log("TestPretreatmentServiceImpl.init")
    }

    override fun onPretreatment(context: Context?, postcard: Postcard?): Boolean {
        log("TestPretreatmentServiceImpl.onPretreatment")
        // 跳转前预处理，如果需要自行处理跳转，该方法返回 false 即可
        //这个例子中我演示的拦截器功能和预处理服务功能是一样的，只不过预处理服务是早于拦截器的
        if (postcard?.path == PATH_ACTIVITY_WEB) {
            if (!ApplicationUtil.instance.isLogin) {
                Toast.makeText(
                    ApplicationUtil.instance.getAppContext(),
                    "还没有登录哦",
                    Toast.LENGTH_SHORT
                ).show()
                routerNavigation(PATH_ACTIVITY_LOGIN)
                return false
            }
        }
        return true
    }
}